package controllers

import play.api.libs.functional.syntax._
import play.api.libs.json.Reads._
import play.api.libs.json._
import services.{LayUIRobotTalk, LayUiRobotMsg}

trait RequestAsJson {

  case class ListParameters(page: Int = 1, limit: Int = 10, query: String = "", active: Option[String] = None)

  case class NasUserInfo(p1: String, p2: String, p3: String)

  //  case class LayUiRobotMsg(id: Long, talk: String, title: String, msg: String, runtime: String, lostTime: String, sendStatus: String)

  //  case class LayUIRobotTalk(id: Long, name: String, access: String, secret: String, note: Option[String], status: String)



  implicit val locationReads: Reads[ListParameters] =
    (JsPath \ "page").read[Int]
      .and((JsPath \ "limit").read[Int])
      .and((JsPath \ "name").read[String])
      .and((JsPath \ "active").readNullable[String])(ListParameters.apply _)

  implicit val locationReadsLayUIRobotTalk: Reads[LayUIRobotTalk] =
    (JsPath \ "id").readNullable[Long]
      .and((JsPath \ "name").readNullable[String])
      .and((JsPath \ "access").readNullable[String])
      .and((JsPath \ "secret").readNullable[String])
      .and((JsPath \ "note").readNullable[String])
      .and((JsPath \ "status").readNullable[String])
      .and((JsPath \ "page").read[Int])
      .and((JsPath \ "limit").read[Int])(LayUIRobotTalk.apply _)

  implicit val lacationReadsLayUiRobotMsg: Reads[LayUiRobotMsg] =
    (JsPath \ "id").read[Long]
      .and((JsPath \ "talk").read[String])
      .and((JsPath \ "title").read[String])
      .and((JsPath \ "msg").read[String])
      .and((JsPath \ "runtime").read[String])
      .and((JsPath \ "losttime").read[String])
      .and((JsPath \ "sendstatus").read[String])
      .and((JsPath \ "page").read[Int])
      .and((JsPath \ "limit").read[Int])(LayUiRobotMsg.apply _)

  def layui_switch(str: Option[String]) = str.getOrElse("") match {
    case "on" => Some(1)
    //    case "" => Some(false)
    case _ => None
  }

}
